package com.ewhome.apartment.mvp.presenter;

import android.app.Application;
import android.content.Intent;

import com.baidu.android.pushservice.PushConstants;
import com.baidu.android.pushservice.PushManager;
import com.baidu.android.pushservice.PushSettings;
import com.ewhome.apartment.BuildConfig;
import com.ewhome.apartment.app.constant.AppConstant;
import com.ewhome.apartment.app.utils.RxUtils;
import com.ewhome.apartment.mvp.model.entity.res.ResApiBean;
import com.ewhome.apartment.mvp.ui.activity.MainTabActivity;
import com.google.gson.Gson;
import com.jess.arms.integration.AppManager;
import com.jess.arms.di.scope.ActivityScope;
import com.jess.arms.integration.cache.Cache;
import com.jess.arms.mvp.BasePresenter;
import com.jess.arms.http.imageloader.ImageLoader;

import io.reactivex.Observable;
import me.jessyan.rxerrorhandler.core.RxErrorHandler;
import me.jessyan.rxerrorhandler.handler.ErrorHandleSubscriber;

import javax.inject.Inject;

import com.ewhome.apartment.mvp.contract.SplashContract;
import com.jess.arms.utils.ArmsUtils;
import com.jess.arms.utils.DataHelper;
import com.jess.arms.utils.PermissionUtil;

import java.util.List;
import java.util.concurrent.TimeUnit;


@ActivityScope
public class SplashPresenter extends BasePresenter<SplashContract.Model, SplashContract.View> {
    private RxErrorHandler mErrorHandler;
    private Application mApplication;
    private ImageLoader mImageLoader;
    private AppManager mAppManager;
    private Gson mGson;
    private Cache<String, Object> apiCache;
    @Inject
    public SplashPresenter(SplashContract.Model model, SplashContract.View rootView
            , RxErrorHandler handler, Application application
            , ImageLoader imageLoader, AppManager appManager,Gson gson,Cache<String, Object> apiCache) {
        super(model, rootView);
        this.mErrorHandler = handler;
        this.mApplication = application;
        this.mGson=gson;
        this.mImageLoader = imageLoader;
        this.apiCache=apiCache;
        this.mAppManager = appManager;
    }
    public void requestPhoneState(){
        PermissionUtil.readPhonestate(new PermissionUtil.RequestPermission() {
            @Override
            public void onRequestPermissionSuccess() {
                PushSettings.enableDebugMode(mApplication, BuildConfig.LOG_DEBUG);
                PushManager.startWork(mApplication, PushConstants.LOGIN_TYPE_API_KEY, AppConstant.BAIDU_PUSH_APIKEY);
            }

            @Override
            public void onRequestPermissionFailure(List<String> permissions) {

            }

            @Override
            public void onRequestPermissionFailureWithAskNeverAgain(List<String> permissions) {

            }
        },mRootView.getRxPermissions(),mErrorHandler);
    }
//    获取所有请求接口，并储存起来
    public void getApis(){
        mModel.getAllApis().compose(RxUtils.applySchedulers(mRootView))
                .subscribe(new ErrorHandleSubscriber<ResApiBean>(mErrorHandler) {
                    @Override
                    public void onNext(ResApiBean resApiBean) {
                        if (resApiBean!=null){
//                            ArmsUtils.snackbarText(mGson.toJson(resApiBean));
                            apiCache.put(AppConstant.API_CACHE,resApiBean);
                        }
                    }
                });
    }

    public void lanuchActivity(){
//        -1表示第一次进入app，0表示不是第一次
        int isFristinApp= DataHelper.getIntergerSF(mApplication,AppConstant.FIRST_IN_APP);
        Observable.just(isFristinApp).delaySubscription(2,TimeUnit.SECONDS)
                .subscribe(new ErrorHandleSubscriber<Integer>(mErrorHandler) {
                    @Override
                    public void onNext(Integer integer) {
                        if (isFristinApp==-1){
                            Intent intent=new Intent(mRootView.getActivity(), MainTabActivity.class);
                            ArmsUtils.startActivity(intent);
                        }else {

                        }
                    }
                });

    }
    @Override
    public void onDestroy() {
        super.onDestroy();
        this.mErrorHandler = null;
        this.mAppManager = null;
        this.mImageLoader = null;
        this.mApplication = null;
    }

}
